package com.chanyachting.emall.serviceImpl;

import java.util.List;

import org.hibernate.Query;

import com.chanyachting.emall.dao.RentyachtDAO;
import com.chanyachting.emall.domain.Rentyacht;
import com.chanyachting.emall.service.RentyachtService;

public class RentyachtServiceImpl implements RentyachtService {

	private RentyachtDAO rDao;

	public RentyachtServiceImpl() {

		this.rDao = new RentyachtDAO();
	}
	
	public int QueryAllRentyachtCountByChannelId(String channelId) {
		String sql = "select count(*) from Rentyacht where recommend = '1' and channelId in (" + channelId + ")";

		return (Integer)this.rDao.find(sql).get(0);
	}
	
	@SuppressWarnings("unchecked")
	public List<Rentyacht> QueryAllRentyacht(int currentPage,int pageSize,String channelId) {
		String sql = "from Rentyacht where recommend = '1' and channelId in (" + channelId + ")";
		sql = sql + " order by updateDate desc";
		Query q = this.rDao.createQuery(sql);
		q.setMaxResults(pageSize);
		q.setFirstResult((currentPage -1) * pageSize);
		
		return q.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<Rentyacht> QueryAppointRentyacht(int currentPage,int pageSize,String productCategoryId) {
		String sql= "from Rentyacht where " +
				"status = '01' and productCategoryId = '" + productCategoryId +"'";
		Query q = this.rDao.createQuery(sql);
		q.setMaxResults(pageSize);
		q.setFirstResult((currentPage -1) * pageSize);
		
		return q.list();
	}
	
	public int QueryAppointRentyachtCount(String status,String productCategoryId) {
		String sql = "select count(*) from Rentyacht " +
			"where status = ? and productCategoryId = ?";
		
		return (Integer)this.rDao.find(sql, new String[]{status,productCategoryId}).get(0);
	}
	
	@SuppressWarnings("unchecked")
	public Rentyacht QueryOneRentyacht(String productId) {
		String sql = "from Rentyacht where id = ?";
		
		List<Rentyacht> l = this.rDao.find(sql, new String[]{productId});
		
		if (l == null || l.size() != 1) {
			return null;
		}
		return l.get(0);
	}
	
	@SuppressWarnings("unchecked")
	public List<Rentyacht> QueryRentyachtByCompanyId(String companyId) {
		String sql = "from Rentyacht where companyId = ? and status = '01'";
		
		List<Rentyacht> l = this.rDao.find(sql, new String[]{companyId});
		
		return l;
	}
}
